<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>刘幸娟小仙女永远开开心心</title>
    <style>
        canvas {
            background-color: black;
            /* border: 1px red solid; */
        }

        body {
            margin: 0;
            overflow: hidden;
        }

        marquee {
            font-weight: bolder;
            font-size: 40px;
            color: white;
        }

    </style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>

</html>
<script>

    let canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d'),
        cw = window.innerWidth,
        ch = window.innerHeight,
        fireworks = [],
        particles = [],
        hue = 120,
        mx, my,
        limiterTotal = 80,
        limiterNow = 0;
    canvas.width = cw;
    canvas.height = ch;

    function random(min, max) {
        return Math.random() * (max - min) + min;
    }

    function calculateDistance(p1x, p1y, p2x, p2y) {
        var xDistance = p1x - p2x,
            yDistance = p1y - p2y;
        return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
    }


    //Firework class
    function Firework(sx, sy, tx, ty) {
        this.x = sx;
        this.y = sy;
        this.sx = sx;
        this.sy = sy;
        this.tx = tx;
        this.ty = ty;
        this.distanceToTarget = calculateDistance(sx, sy, tx, ty);
        this.distanceTraveled = 0;
        this.angle = Math.atan2(ty - sy, tx - sx);
        this.speed = 2;
        this.acceleration = 1.05;
        this.brightness = random(50, 70);
        this.targetRadius = 1;
        this.coordinateCount = 3;
        this.coordinates = []
        while (this.coordinateCount--) {
            this.coordinates.push([this.x, this.y]);
        }
    }

    Firework.prototype.draw = function () {
        ctx.beginPath();
        ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1])
        ctx.lineTo(this.x, this.y)
        ctx.strokeStyle = 'hsl(' + hue + ',100%,' + this.brightness + '%)';//这里要对变量进行拼接
        ctx.stroke();
        ctx.beginPath();
        ctx.arc(this.tx, this.ty, this.targetRadius, 0, Math.PI * 2);
        ctx.stroke();
    }

    Firework.prototype.update = function (index) {
        this.coordinates.pop()
        this.coordinates.unshift([this.x, this.y])
        if (this.targetRadius < 8) {
            this.targetRadius += 0.3
        } else {
            this.targetRadius = 1
        }
        this.speed = this.speed * this.acceleration;
        let vx = Math.cos(this.angle) * this.speed,
            vy = Math.sin(this.angle) * this.speed;
        this.distanceTraveled = calculateDistance(this.sx, this.sy, this.x + vx, this.y + vy)
        if (this.distanceTraveled >= this.distanceToTarget) {
            fireworks.splice(index, 1)
            createParticle(this.tx, this.ty)
        } else {
            this.x += vx;
            this.y += vy;
        }
    }

    function Particle(x, y) {
        this.x = x;
        this.y = y;
        this.angle = random(0, Math.PI * 2);
        this.speed = random(1, 10)
        this.friction = 0.95
        this.gravity = 1;
        this.hue = random(hue - 20, hue + 20)
        this.brightness = random(50, 80)
        this.alpha = 1;
        this.decay = random(0.015, 0.03)
        this.coordinates = [];
        this.coordinateCount = 5;
        while (this.coordinateCount--) {
            this.coordinates.push([this.x, this.y])
        }
    }

    Particle.prototype.draw = function () {
        ctx.beginPath();
        ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1])
        ctx.lineTo(this.x, this.y)
        ctx.strokeStyle = 'hsla(' + hue + ',100%,' + this.brightness + '%,' + this.alpha + ')';//这里要对变量进行拼接,加一个透明变量进来
        ctx.stroke();
    }
    Particle.prototype.update = function (index) {
        this.coordinates.pop()
        this.coordinates.unshift([this.x, this.y])
        this.speed *= this.friction;
        this.x += Math.cos(this.angle) * this.speed;
        this.y += Math.sin(this.angle) * this.speed + this.gravity;
        this.alpha -= this.decay;
        if (this.alpha <= this.decay) {
            particles.splice(index, 1)
        }
    }

    function createParticle(x, y) {
        let partcicleCount = 100
        while (partcicleCount--) {
            particles.push(new Particle(x, y))
        }
    }

    function run() {
        hue++
        ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
        ctx.fillRect(0, 0, cw, ch);
        var i = fireworks.length;
        while (i--) {
            fireworks[i].draw();
            fireworks[i].update(i);
        }
        var k = particles.length;
        while (k--) {
            particles[k].draw();
            particles[k].update(k);
        }
        if (limiterNow >= limiterTotal) {
            fireworks.push(new Firework(cw / 2, ch, random(0, cw), random(0, ch / 2)))
            limiterNow = 0;
        } else {
            limiterNow++
        }
    }

    canvas.addEventListener('mousedown', function (e) {
        mx = e.pageX - canvas.offsetLeft;
        my = e.pageY - canvas.offsetTop;
        fireworks.push(new Firework(cw / 2, ch, mx, my))
    })
    setInterval(run, 20)//每隔33毫秒刷新一下屏幕
</script>